package com.springcloud.market.stock.controller;


import com.springcloud.market.common.dto.OrderDTO; // ✅ 必须是 common 包

import com.springcloud.market.common.feign.OrderFeignService;
import com.springcloud.market.common.feign.StockDeductDTO;
import com.springcloud.market.stock.service.StockService;
import com.springcloud.market.common.model.R;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RefreshScope
@RequestMapping("/stock")
@RequiredArgsConstructor
public class StockController {

//    @Value("${user.name}")
//    private String name;
//    @Value("${user.age}")
//    private Integer age;
//
//
//    @RequestMapping("/info")
//    public String info(){
//        return "姓名："+name+". 年龄："+age;
//    }

    private final StockService stockService;
    private final OrderFeignService orderFeignService;



    @RequestMapping("/reduce")
    public String reduce() {
        System.out.println("扣减库存");
        return "扣减库存";
    }

    @RequestMapping("/readStock")
    public String readStock(){
        System.out.println("网关测试：stock服务");
        return "网关测试：stock服务";
    }

    @PostMapping("/reduceStock")
    public R<String> reduceStock(@RequestParam("skuId") Long skuId,
                                 @RequestParam("num") Integer num) {
        boolean ok = stockService.deduct(skuId,num);
        if (!ok) return R.fail("库存不足");

        OrderDTO dto = new OrderDTO();
        dto.setSkuId(skuId);
        dto.setNum(num);

        return orderFeignService.createOrder();//远程调用
    }


    @PostMapping("/deduct")
    public R<Void> deduct(@RequestBody StockDeductDTO dto) {
        //业务逻辑
        return R.ok();
    }


}
